<?php

    class DateUtil
    {
        const HARDCODE_DATE     = '';
        const HARDCODE_TIME     = '';
     
        /**
         * Convert your date into another format
         * @param string $format convert format
         * @param string $date original date value
         * @return string date with new format 
         */
        public static function convertDate($format, $date)
        {
            if ($date != null)
            {
                $time = strtotime($date);
                if ($time !== false)
                {
                    return date($format, $time);
                }
            }
            return '';
        }

        public static function timeDiffInMinutes($date1,$date2){
            $to_time = strtotime($date1);
            $from_time = strtotime($date2);
            return round(abs($to_time - $from_time) / 60);
        }

        /**
         * Calculate how much time has passed for a given datetime compare with current datetime
         * @param string $date
         * @return string 
         */
        public static function elapseTime($date)
        {

            $time = strtotime($date);
            $time = time() - $time; 

            $tokens = array(
                31536000 => 'năm',
                2592000 => 'tháng',
                604800 => 'tuần',
                86400 => 'ngày',
                3600 => 'giờ',
                60 => 'phút',
                1 => 'giây'
            );
            $result = '';
            foreach ($tokens as $unit => $text)
            {
                if ($time < $unit)
                    continue;
                $numberOfUnits = floor($time / $unit);
                $result = $numberOfUnits . ' ' . $text;
                break;
            }

            if($result!=''){
                return $result;    
            }
            return '0 giây';
        }

        /**
         * return standard format of current datetime 
         * @return string Standard date time
         */
        public static function getDefaultCurrentDateTime()
        {
            if(self::HARDCODE_DATE!=''){
                return self::HARDCODE_DATE;
                
            }else{
                return date('Y-m-d H:i:s');
            }
        }

        public static function getDefaultYear(){
            if(self::HARDCODE_DATE!='' && self::canUserSetDate()){
                $dateArray = explode('-',self::HARDCODE_DATE);
                return $dateArray[0];
            }else{
                return date('Y');
            }
        }

        public static function getDefaultCurrentTime(){
            if(self::HARDCODE_TIME !='' && self::canUserSetDate()){
                return self::HARDCODE_TIME;
            }else{
                return date('H:i:s');
            }
        }

        public static function getDefaultPeriod(){
            if(self::HARDCODE_DATE != '' && self::canUserSetDate()){
                $dateArray = explode('-',self::HARDCODE_DATE);
                return $dateArray[0].$dateArray[1];
            }else{
                return date('Ym');
            }
        }

        public static function haveDateSet(){
            return (self::HARDCODE_DATE !=null || self::HARDCODE_TIME!=null) && self::canUserSetDate();
        }

        protected static function canUserSetDate(){
            $list = array('michael.bonza@qsibanking.com');
            foreach($list as $email){
                if(Yii::app()->user->isGuest == false && Yii::app()->user->model->email == $email){
                    return true;
                }
            }
            return false;
        }
    }
